package com.atguigu.tingshu.account.service.impl;
import java.math.BigDecimal;
import java.util.Date;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.atguigu.tingshu.account.mapper.UserAccountDetailMapper;
import com.atguigu.tingshu.account.mapper.UserAccountMapper;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.execption.TingShuException;
import com.atguigu.tingshu.model.account.UserAccountDetail;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.atguigu.tingshu.account.mapper.RechargeInfoMapper;
import com.atguigu.tingshu.account.service.RechargeInfoService;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.account.RechargeInfo;
import com.atguigu.tingshu.vo.account.RechargeInfoVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

@Service
@SuppressWarnings({"all"})
public class RechargeInfoServiceImpl extends ServiceImpl<RechargeInfoMapper, RechargeInfo> implements RechargeInfoService {

	@Autowired
	private RechargeInfoMapper rechargeInfoMapper;

	@Autowired
	private UserAccountMapper userAccountMapper;

	@Autowired
	private UserAccountDetailMapper userAccountDetailMapper;

	/**
	 * 充值金额
	 * @param rechargeInfoVo
	 * @return
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public Map<String, String> submitRecharge(RechargeInfoVo rechargeInfoVo) {
		Long userId = AuthContextHolder.getUserId();
		RechargeInfo rechargeInfo = new RechargeInfo();
		String orderNo = "CZ" + DateUtil.today() + IdUtil.getSnowflakeNextId();
		rechargeInfo.setUserId(userId);
		rechargeInfo.setOrderNo(orderNo);
		rechargeInfo.setRechargeStatus(SystemConstant.ORDER_STATUS_UNPAID);
		rechargeInfo.setRechargeAmount(new BigDecimal("0"));
		rechargeInfo.setRechargeAmount(rechargeInfoVo.getAmount());
		rechargeInfo.setPayWay(rechargeInfoVo.getPayWay());
		this.save(rechargeInfo);
		Map<String,String> map = new HashMap<>();
		map.put("orderNo",orderNo);
		return map;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void rechargePaySuccess(String orderNo) {
		RechargeInfo rechargeInfo = this.getOne(new LambdaQueryWrapper<RechargeInfo>().eq(RechargeInfo::getOrderNo, orderNo));
		if(rechargeInfo != null && rechargeInfo.getRechargeStatus().equals(SystemConstant.ORDER_STATUS_PAID)){
			throw new TingShuException(400,"余额已支付");
		}
		rechargeInfo.setRechargeStatus(SystemConstant.ORDER_STATUS_PAID);
		rechargeInfo.setUpdateTime(new Date());
		this.updateById(rechargeInfo);
		//更新用户账单表
		Integer count = userAccountMapper.updateUserAccount(rechargeInfo.getRechargeAmount(),rechargeInfo.getUserId());
		if(count == 0){
			throw new TingShuException(500,"更新用户账户失败");
		}
		//保存用户账户明细
		UserAccountDetail userAccountDetail = new UserAccountDetail();
		userAccountDetail.setUserId(rechargeInfo.getUserId());
		userAccountDetail.setTitle("【账户充值】，充值金额:" + rechargeInfo.getRechargeAmount());
		userAccountDetail.setTradeType(SystemConstant.ACCOUNT_TRADE_TYPE_DEPOSIT);
		userAccountDetail.setAmount(rechargeInfo.getRechargeAmount());
		userAccountDetail.setOrderNo(orderNo);
		userAccountDetailMapper.insert(userAccountDetail);
	}
}
